package com.crape.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.crape.account.bo.IUploadBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.constants.ConstantCode;
import com.crape.common.constants.MessageCode;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.ImageInfo;
import com.crape.common.model.Subject;
import com.crape.common.thread.ThreadObjManager;
import com.crape.topic.bo.ISubjectBO;

@Controller
public class AdminTopicController {

    private ISubjectBO subjectBOImpl;
    private IUploadBO  uploadBOImpl;

    @Autowired
    public void setSubjectBOImpl(ISubjectBO subjectBOImpl) {
        this.subjectBOImpl = subjectBOImpl;
    }
    
    @Autowired
	public void setUploadBOImpl(IUploadBO uploadBOImpl) {
		this.uploadBOImpl = uploadBOImpl;
	}
    
    @RequestMapping(value="/admin/topic/addSubjectImages.jsp" ,method=RequestMethod.POST)
	public String addSubjectImages(HttpServletRequest request, @RequestParam("fileUpload")MultipartFile fileUpload, ModelMap modelMap, Subject subject) {
		try {
			ImageInfo imageInfo = uploadBOImpl.uploadImage(request, fileUpload, ConstantCode.imageSize);
			subject.setImagename(imageInfo.getName());
			modelMap.put("imageInfo", imageInfo);
		} catch (BOException e) {
			e.printStackTrace();
			modelMap.put("message", e.getMessage());
		}
		modelMap.put("subject", subject);
		return "/admin/topic/addSubject.html";
    }

    @RequestMapping("/admin/topic/addSubject.jsp")
    public String addSubject(ModelMap modelMap, Subject subject) {
        if (subject == null) {
            modelMap.put(MessageCode.key, MessageCode.PARAMS_IS_NULL);
            return "/admin/topic/addSubject.html";
        }
        LoginUser loginUser = ThreadObjManager.getLoginUser();
        subject.setAccountid(loginUser.getAccountid());
        subjectBOImpl.addSubject(subject);
        modelMap.put("subject", subject);
        modelMap.put(MessageCode.key, MessageCode.SUCCES_SAVE);
        return "/admin/topic/addSubject.html";
    }

    @RequestMapping("/admin/topic/subjectList.jsp")
    public String subjectQuery(ModelMap modelMap, Subject subject) {
        List<Subject> subjectList = subjectBOImpl.listSubjects(subject);
        modelMap.put("subjectList", subjectList);
        modelMap.put("subject", subject);
        return "/admin/topic/listSubject.html";
    }

    @RequestMapping("/admin/topic/updateInit.jsp")
    public String updateInit(ModelMap modelMap, Subject subject, Long updateId,Integer updateStatus) {
        if (updateId == null) {
            modelMap.put(MessageCode.key, MessageCode.PARAMS_IS_NULL);
            return this.subjectQuery(modelMap, subject);
        }
        Subject params = new Subject();
        params.setId(updateId);
        List<Subject> subjectList = subjectBOImpl.listSubjects(params);
        if (subjectList == null) {
            return this.subjectQuery(modelMap, subject);
        }
        modelMap.put("subject", subjectList.get(0));
        return "/admin/topic/modiSubject.html";
    }

    @RequestMapping("/admin/topic/statusUpdate.jsp")
    public String statusUpdate(ModelMap modelMap, Subject subject, Long updateId,Integer updateStatus) {
        if (updateId == null || updateStatus == null) {
            modelMap.put(MessageCode.key, MessageCode.PARAMS_IS_NULL);
            return this.subjectQuery(modelMap, subject);
        }
        Subject params = new Subject();
        params.setId(updateId);
        params.setStatus(updateStatus);
        subjectBOImpl.updateSubject(params);
        modelMap.put(MessageCode.key, MessageCode.SUCCES_UPDATE);
        return this.subjectQuery(modelMap, subject);
    }
    
    @RequestMapping(value="/admin/topic/updateSubjectImages.jsp" ,method=RequestMethod.POST)
	public String uploadSubjectImages(HttpServletRequest request, @RequestParam("fileUpload")MultipartFile fileUpload, ModelMap modelMap, Subject subject) {
		try {
			ImageInfo imageInfo = uploadBOImpl.uploadImage(request, fileUpload, ConstantCode.imageSize);
			subject.setImagename(imageInfo.getName());
			modelMap.put("imageInfo", imageInfo);
		} catch (BOException e) {
			e.printStackTrace();
			modelMap.put("message", e.getMessage());
		}
		modelMap.put("subject", subject);
		return "/admin/topic/modiSubject.html";
    }

    @RequestMapping("/admin/topic/subjectUpdate.jsp")
    public String subjectUpdate(ModelMap modelMap, Subject subject) {
        if (subject == null || subject.getId() == null || subject.getTitle() == null
            || subject.getDescription() == null) {
            modelMap.put(MessageCode.key, MessageCode.PARAMS_IS_NULL);
            return "/admin/topic/modiSubject.html";
        }
        subjectBOImpl.updateSubject(subject);
        modelMap.put(MessageCode.key, MessageCode.SUCCES_UPDATE);
        return "/admin/topic/modiSubject.html";
    }
    
}
